package com.coocaa.typhoon.api.v1.redis;

import com.alibaba.fastjson.JSONObject;
import com.coocaa.cirrus.common.base.model.RespBase;
import com.coocaa.typhoon.api.base.constant.CacheConstants;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @author bijiahao
 * @date : 2025/10/21 15:36
 * @description
 */
@RestController
@RequestMapping("/v1/redis")
@Tag(name = "Redis测试")
public class TestRedisController {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private static final String TEST_ID = "test";

    @Operation(summary = "测试写入Redis")
    @PostMapping(value = "/writeRedis")
    public RespBase<Object> writeRedis(@RequestBody JSONObject data) {
        redisTemplate.opsForValue().set(TEST_ID, data, CacheConstants.CACHE_TIME_SIX_MIN, TimeUnit.SECONDS);
        return new RespBase().setSuccessResult();
    }

    @Operation(summary = "测试读取Redis")
    @PostMapping(value = "/readRedis")
    @Parameters({
            @Parameter(name = "id", example = "test", description = "ID")
    })
    public RespBase<Object> readRedis(@RequestParam String id) {
        Object res = redisTemplate.opsForValue().get(id);
        return new RespBase().setData(res).setSuccessResult();
    }
}
